/*
 * Copyright 2014 trinea.cn All right reserved. This software is the confidential and proprietary information of
 * trinea.cn ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with trinea.cn.
 */
package com.ihighest.app.infox.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ihighest.app.common.kit.AppKit;
import com.jakewharton.salvage.RecyclingPagerAdapter;

/**
 * ImagePagerAdapter
 * 
 * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2014-2-23
 */
public class ImagePagerAdapter extends RecyclingPagerAdapter {

	Context context;
	List<Integer> imageIdList;
	String[] imageUrls;

	int size;
	boolean isInfiniteLoop;

	//
	// ImageLoader imageLoader;
	// DisplayImageOptions defaultOptions;

	public ImagePagerAdapter(Context context, List<Integer> imageIdList) {
		this.context = context;
		this.imageIdList = imageIdList;
		this.size = imageIdList != null ? imageIdList.size() : 0;
		isInfiniteLoop = false;
	}

	public ImagePagerAdapter(Context context, String[] imageUrls) {
		this.context = context;
		this.imageUrls = imageUrls;
		this.size = imageUrls != null ? imageUrls.length : 0;
		isInfiniteLoop = false;
		// imageLoader = ImageLoader.getInstance();
		// defaultOptions = new
		// DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.infox_img_loadrong)
		// .showImageOnFail(R.drawable.infox_img_loadrong).showImageOnLoading(R.drawable.infox_img_loadrong).cacheInMemory(true)
		// .cacheOnDisc(true).build();
	}

	@Override
	public int getCount() {
		if (imageIdList != null) {
			return isInfiniteLoop ? Integer.MAX_VALUE : (imageIdList != null ? imageIdList.size() : 0);
		} else if (imageUrls != null) {
			return isInfiniteLoop ? Integer.MAX_VALUE : (imageUrls != null ? imageUrls.length : 0);
		} else {
			return 0;
		}
	}

	/**
	 * get really position
	 * 
	 * @param position
	 * @return
	 */
	private int getPosition(int position) {
		return isInfiniteLoop ? position % size : position;
	}

	@Override
	public View getView(int position, View view, ViewGroup container) {
		ViewHolder holder;
		if (view == null) {
			holder = new ViewHolder();
			view = holder.imageView = new ImageView(context);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		if (imageIdList != null) {
			holder.imageView.setImageResource(imageIdList.get(getPosition(position)));
		} else {
			// imageLoader.displayImage(imageUrls[getPosition(position)],
			// holder.imageView, defaultOptions);2
			AppKit.SetNormalImg(imageUrls[getPosition(position)], holder.imageView);
		}
		return view;
	}

	private static class ViewHolder {

		ImageView imageView;
	}

	/**
	 * @return the isInfiniteLoop
	 */
	public boolean isInfiniteLoop() {
		return isInfiniteLoop;
	}

	/**
	 * @param isInfiniteLoop
	 *            the isInfiniteLoop to set
	 */
	public ImagePagerAdapter setInfiniteLoop(boolean isInfiniteLoop) {
		this.isInfiniteLoop = isInfiniteLoop;
		return this;
	}
}
